package GUI;


import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import client.ClientConsole;

import Library.Search;

/**
 * This class for Search
 */
public class GUI_Search extends JPanel
{
	/**
	 * Text field for word to search
	 */
	private JTextField searchText;
	/**
	 * Combo Box
	 */
	private JComboBox searchBox = null;
	/**
	 * search by name/subject/free word
	 */
	private String searchType = "0";
	/**
	 * client Connected
	 */
	private ClientConsole cc;
	/**
	 * panel for search
	 */
	private GUI_Search gs = this;
	/**
	 * panel for search
	 */
	private JPanel backPanel;
	/**
	 * @param clc 
	 * @param mm  
	 */
	public GUI_Search(ClientConsole clc, final GUI_ManagerMenu mm)
	{
		cc = clc;
		this.setSize(600, 400);
		backPanel=mm;
		same();
	}
	/**
	 * @param clc 
	 * @param mms  
	 */
	public GUI_Search(ClientConsole clc, final GUI_MainMenuStudent mms)
	{
		super();	
		cc = clc;
		this.setSize(600, 700);
		backPanel=mms;
		same();
	}
	/**
	 * @param clc 
	 * @param ml  
	 */
	public GUI_Search(ClientConsole clc, final GUI_LibrarianMenu lm)
	{
		super();	
		cc = clc;
		this.setSize(600, 700);
		backPanel=lm;
		same();
	}
	
	public void same()
	{
		JLabel lblTitle = new JLabel("Search Book ");
		lblTitle.setFont(new Font("Tahoma", Font.PLAIN, 24));

		lblTitle.setBounds(230, 20, 200, 40);
		add(lblTitle);
		
		searchText = new JTextField();
		searchText.setBounds(60, 110, 120, 25);
		add(searchText);
		searchText.setColumns(10);
		
		this.add(getSearchBox(), null);
		
		JButton btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String text=searchText.getText();
				String s[]={"search",text,searchType};
				try {
					cc.client.sendToServer(s);
					while(cc.client.flag==0)
					    System.out.print("");
					    cc.client.flag=0;
					    Search search =(Search)cc.client.message;
					    if(search.booksList.isEmpty())
							JOptionPane.showMessageDialog(new JFrame(),"No books were find");
					    else
					    {
					    	GUI_ShowBooks sb= new GUI_ShowBooks(cc,search.booksList,text,gs);
					    	cc.cgui.setContentPane(sb);
					    }
					    	
				} catch (IOException e) {
					e.printStackTrace();
				}	// send message to server

				
		}});
		
		JButton btnBack = new JButton("Back");
		
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cc.cgui.setContentPane(backPanel);
			}
		})
		;
		btnBack.setBounds(240, 320, 120, 30);
		add(btnBack);
	setLayout(null);
	btnSearch.setBounds(60, 320, 120, 30);
	this.add(btnSearch);
	this.setVisible(true);		
	}
	

	private JComboBox getSearchBox() {
		if (searchBox == null) {
			searchBox = new JComboBox();
			searchBox.setBounds(new Rectangle(60, 210, 120, 25));  
				
			searchBox.addItem("by name");
			searchBox.addItem("by subject");
			searchBox.addItem("by free word");

				
			searchBox.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					Integer temp = searchBox.getSelectedIndex();
					searchType = temp.toString();
				}
			});
		}
		return searchBox;
	}
}